package com.winner.data.util.excel;

import java.util.Set;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;

/**
 * easyexcel自定义行列样式 背景色+字体颜色
 *
 * @author wangxinle5
 * @since 2023-02-11
 */
public class CustomCellWriteHandler implements CellWriteHandler {

    // 字体颜色
    private Short colorIndex;
    // 背景颜色
    private Short bgColorIndex;
    // 需要染色的行列下标拼接(第一行第一列, 那么就是 00)
    private Set<String> colorSet;

    /**
     *
     * @param bgColorIndex -1 不设置 背景颜色
     * @param colorIndex -1 不设置 字体颜色
     * @param colorList value = -1,整行
     */
    public CustomCellWriteHandler(Short bgColorIndex, Short colorIndex, Set<String> colorList) {
        this.colorIndex = colorIndex;
        this.bgColorIndex = bgColorIndex;
        this.colorSet = colorList;
    }

    /**
     * 自定义单元格创建前的操作
     */
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
        Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        // 设置行高
        short height = 600;
        row.setHeight(height);
    }

    /**
     * 单元格处理后(已写入值)的操作
     */
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        Row row = cell.getRow();
        if (colorSet.contains(row.getRowNum() + String.valueOf(cell.getColumnIndex()))) {
            setCellStyle(context, cell);
        }
        // 染色条件：第1行，第1列，或者其他自定义条件
        // if (row.getRowNum() == 0 && cell.getColumnIndex() == 0) {
        // setCellStyle(context, cell);
        // }
    }

    private void setCellStyle(CellWriteHandlerContext context, Cell cell) {
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        // 设置单元格背景颜色--红色/橙色
        cellStyle.setFillForegroundColor(bgColorIndex);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 自动换行
        cellStyle.setWrapText(true);
        // 设置左右对齐为居中对齐
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);
        cellStyle.setBorderRight(BorderStyle.MEDIUM);
        cellStyle.setBorderTop(BorderStyle.MEDIUM);
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setBold(true);
        // 设置字体颜色 白色
        font.setColor(colorIndex);
        cellStyle.setFont(font);
        // 样式写入单元格
        cell.setCellStyle(cellStyle);
        // 这里要把 WriteCellData的样式清空，不然后面还有一个拦截器 FillStyleCellWriteHandler 默认会将WriteCellStyle设置到cell里面去,会导致自己设置的不一样
        context.getFirstCellData().setWriteCellStyle(null);
    }

}
